<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;

class resetcontroller extends Controller
{
    //
    public function forget()
    {
    	return view('admin.reset.email');
    }

    public function send(Request $request)
    {
    	// Mail::raw('发送一封邮件', function($message){
    	// 	$message -> to('2452801061@qq.com');
    	// 	$message -> subject('这是邮箱的主题');
    	// });
    	
    	// Mail::send('email.forget',[], function($message){
    	// 	$message -> to('2452801061@qq.com');
    	// 	$message -> subject('这是邮箱的主题');
    	// });
    	
    	$email = $request -> input('email');
    	$res = DB::table('users') -> where('email',$email) -> first();

    	if(!$res)
    	{
    		return back() -> with(['info' => '邮箱不存在'] );
    	}

    	Mail::send('email.forget',['res' => $res], function($message) use ($email){
    		$message -> to($email);
    		$message -> subject('这是邮箱的主题');
    	});
    }

    public function find($uid)
    {
    	$res = DB::table('users') -> where('uid',$uid) -> first();

    	if(!$res)
    	{
    		return redirect('/admin/reset/info') -> with(['info' => '不是一个合法的来源，请走开'] );
    	}

    	return view('admin.reset.newpass',['res' => $res] );

    }

    public function info()
    {
    	return view('admin.reset.info');
    }

    public function uppass(Request $request)
    {
    	$data = $request -> except('_token');

    	$pwd = Crypt::encryptString($data['upwd']);
    	$res = DB::table('users') -> where('email',$data['email']) -> update(['upwd' => $pwd]);

    	if($res)
    	{
    		return redirect('/admin/login') -> with(['info' => '修改成功'] );
    	}else
    	{
    		return back() -> with(['info' => '修改失败'] );
    	}
    }
}
